﻿Imports System.IO

Public Class frmmain

    Private Sub GetFiles(ByVal PathName As String)
        Dim LItem As ListViewItem
        Dim icoIdx As Integer = 2

        'Clear listview.
        Call LstFiles.Items.Clear()

        For Each nFile In Directory.GetFiles(PathName)
            'Remove this if you want to add hidden files to.
            If (File.GetAttributes(nFile) And FileAttributes.Hidden) = False Then
                'Create new listview item.
                LItem = New ListViewItem()

                'Set icon type
                If nFile.ToLower().EndsWith(".exe") Then
                    icoIdx = 3
                Else
                    icoIdx = 2
                End If

                With LItem
                    .ImageIndex = icoIdx
                    .Tag = Path.Combine(PathName, nFile)
                    .Text = New FileInfo(nFile).Name
                End With

                'Add to listitem to listview
                Call LstFiles.Items.Add(LItem)
            End If
        Next

    End Sub

    Private Sub GetFolders(ByVal PathName As TreeNode)
        Dim di As DirectoryInfo

        'Clear nodes
        Call PathName.Nodes.Clear()

        'Get folder path.
        di = New DirectoryInfo(PathName.Tag)

        Try
            'List folders in treeview.
            For Each nFolder In di.GetDirectories()
                'Create new node.
                Dim tNode As New TreeNode()

                With tNode
                    .ImageIndex = 1
                    .SelectedImageIndex = 1
                    .Tag = nFolder.FullName
                    .Text = nFolder.Name.ToString()
                    .Nodes.Add("")
                End With

                'Get folder info.
                di = New DirectoryInfo(tNode.Tag)

                'Remove this if you want to add hidden fiolders to.
                If (di.Attributes And FileAttributes.Hidden) = False Then
                    'Add the node
                    Call PathName.Nodes.Add(tNode)
                End If

            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub GetDrives()

        For Each Drive In DriveInfo.GetDrives

            'Make sure drive is ready.
            If (Drive.IsReady) Then
                'Get drive volume label.
                Dim dName As String = Drive.VolumeLabel
                'If not lebel is found use drive Letter
                If String.IsNullOrEmpty(dName) Then
                    dName = Drive.Name
                End If

                'Create node.
                Dim tNode As New TreeNode()

                With tNode
                    .ImageIndex = 0
                    .SelectedImageIndex = 0
                    .Text = dName
                    .Nodes.Add("")
                    .Tag = Drive.Name
                End With
                'Add the node to treeview.
                Call Tv1.Nodes.Add(tNode)
            End If
        Next

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'List drives in treeview.
        Call GetDrives()
    End Sub

    Private Sub Tv1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles Tv1.BeforeExpand
        'List folders in treeview.
        Call GetFolders(e.Node)
    End Sub

    Private Sub Tv1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles Tv1.AfterSelect
        Call GetFiles(e.Node.Tag)
        'Update label caption.
        lblFilename.Text = e.Node.Tag
    End Sub

    Private Sub LstFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstFiles.SelectedIndexChanged
        Dim LItem As New ListViewItem

        For Each LItem In LstFiles.SelectedItems
            'Update label caption.
            lblFilename.Text = LItem.Tag
        Next

    End Sub

    Private Sub cmdAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAbout.Click
        'Show messagebox.
        Call MsgBox(Text & vbCrLf & vbTab & "By Ben Jones.", vbInformation Or vbOKOnly, "About . . .")
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        'Clsoe program
        Call Close()
    End Sub
End Class
